/*******************************************************************************
 * CAROL CHOU
 * RA8806 / 240*160 / Winbond
 ********************************************************************************/
#ifndef __RA8806_H__
  #define __RA8806_H__

  #include <stdio.h>
  /*#include <intrins.h>
  #include "W78E516B.H"*/
  #include "RA8806Reg.h"
  #include "UserDef.h"	  
  //------------------------------------------------------------------------------
  //Complier Defines
  //------------------------------------------------------------------------------
  //===== LCD Panel Size =====
  #ifdef P320x240
    #define cAWRR		39	
    #define cAWBR		239     
    #define cDWRR 		39      
    #define cDWBR		239
    #define STN_Width   320
    #define STN_Height  240
  #endif 

  #ifdef P240x160
    #define cAWRR		29	
    #define cAWBR		159     
    #define cDWRR 		29      
    #define cDWBR		159
    #define STN_Width   240
    #define STN_Height  160
  #endif 

  #ifdef BIG5_Code
    #define cWLCR		B1000_1101	//BIG5 Code	
  #endif 

  #ifdef GB_Code
    #define cWLCR		B1001_1101	//GB Code	
  #endif 

  //------------------------------------------------------------------------------
  //Parameter Defines
  //------------------------------------------------------------------------------
  //===== MCU Interface =====
  #ifdef Parallel_8080
    #define DATA_BUS		GPA, 0
    //#define bRST     		P4_1		//New Demo_Board 
    //#define bA0				P4_0		//New Demo_Board

    #define bRST     		GPB, 3		//35 Demo_Board 
#ifdef CONFIG_PLATFORM==Nuvoton_EVB
    #define bA0				GPA, 9		//35 Demo_Board    
#else
    #define bA0				GPC, 7		//35 Demo_Board    
#endif
    #define bINT			GPA, 10

#ifdef CONFIG_PLATFORM==Nuvoton_EVB
    #define bCSZ			GPA, 11
    #define bWRZ			GPA, 12
    #define bRDZ			GPA, 13
#else    
	#define bCSZ			GPC, 14
    #define bWRZ			GPC, 15
    #define bRDZ			GPC, 6
#endif
/*    #define bBUSY		    GPA, 14 
    #define bDB             GPA, 15
    #define bMI			    GPB, 1*/
    //uchar volatile xdata CMD		_at_ 0xfeff;		//Command
    //uchar volatile xdata RAM		_at_ 0xffff;		//Display RAM
  #endif 

#define screen_normal  LCD_CmdWrite(WLCR); \
  LCD_DataWrite(0x04);\
  Check_Val(WLCR, 0x04);
#define screen_inverse  LCD_CmdWrite(WLCR);\
  LCD_DataWrite(0x05); \
  Check_Val(WLCR, 0x05);

#define screen_rotate_180 \
  COM_DIR_239_0();\
  SEG_DIR_319_0();

#define screen_rotate_normal \
  COM_DIR_0_239();\
  SEG_DIR_0_319();

#define Show_text(X, Y, content) \
  LCD_GotoXY(X, Y);\
  Access_Page1();\
  LCD_Text();\
  LCD_ShowStr(content, 0);\
  LCD_Set_Rect(X, Y, 8*strlen(content), 16, 0x00, 2);
#define Show_text1(X, Y, content) \
  LCD_GotoXY(X, Y);\
  Access_Page1();\
  LCD_Text();\
  LCD_ShowStr(content, 0);\
  LCD_Set_Rect(X, Y, 8*strlen(content), 16, 0xff, 2);

/* kb define */
#define Enter_key 0x0d
#define Up_key  'w'
#define Down_key  'x'
#define Power_key 'a'
#define Null_key 0x00

  //Subroutine Declare
  void LCD_CmdWrite(uchar);
  void LCD_DataWrite(uchar);
  extern void LCD_ChkBusy(void);

  //Delay Function
  extern void Delay2us(uchar);
  extern void Delay100us(uchar);
  extern void Delay1ms(uchar);
  extern void Delay10ms(uchar);
  extern void Delay100ms(uchar);

struct Rect
{
  int top_x, left_y, right_x, bottom_y;
};

struct STN_data
{
  char *content;
  int length;
  int disp_x, disp_y; //disp_x  align with 8, disp_x=0~(240/8-1), disp_y=0~159
  int layer; //0:DDRAM1, 1:DDRAM2, 2:DDRAM1&DDRAM2, 3:grayscale mode
  int text_graphic;  //0:text, 1:graphic
  //struct Rect* pROI;
};


struct PackedDate {
						unsigned int Second: 6;		// 0..59

						// higher 32 bits
                        unsigned int Minute : 6;    // 0..59                       63 is reserved as EOD marker
                        unsigned int Hour : 5;      // 0..23                       31 is reserved as EOD marker
                        unsigned int Day : 5;       // 1..31
                        unsigned int Month : 4;     // 1..12
                        unsigned int Year : 12;		// 0..4095
                                             
                    };

// 8 byte (64 bit) date time stamp
union MaestroDate
{
						char	Date[5];
						struct PackedDate PackDate;
};

struct Measure_data {
  double Xenon_I, Non_Xenon_I;
};

#define MAX_Samples 30
struct Measure_tran {
  int tran_index, blank_flag, sample_index;
//  struct Measure_data blank, samples_A230[MAX_Samples], samples_A260[MAX_Samples], samples_A280[MAX_Samples];
  struct Measure_data blank_A230, blank_A260, blank_A280, samples_A230, samples_A260, samples_A280;
  double A230[MAX_Samples], A260[MAX_Samples], A280[MAX_Samples];
  union MaestroDate date[MAX_Samples+1];
  int sample_No[MAX_Samples];
  int last_sample_No;
};

struct Measure_sheet {
  int sheet_type;
  union MaestroDate sheet_date;
};

struct MaestroNano_Flash_t {
  int sheet_type;
  union MaestroDate sheet_date;
  int blank_flag, sample_index;
  double A230[MAX_Samples], A260[MAX_Samples], A280[MAX_Samples];
  union MaestroDate date[MAX_Samples+1];
  int sample_No[MAX_Samples];
  unsigned char check_digits[8];
}; //4+5+8+8*3*30+31*5+4*30
#define icon_dsDNA 0
#define icon_ssDNA 1
#define icon_RNA 2
#define icon_protein 3
#define icon_up_key 4
#define icon_down_key 5
#define icon_enter_key 6
#define icon_yes 7
#define icon_no 8

#define MAX_Sheets 10
#define MAX_keys   15
#define USE_KEY_QUEUE 1

/* I2C RTC device */
#define M41T0_I2C_Slave_Addr 0x68
/* Tx_Data0={An, length, byte0, ...}, An is word address, then follow by data */
extern uint8_t Tx_Data0[10];
extern uint8_t Rx_Data0[10];
extern uint8_t DataLen0;
extern volatile uint8_t EndFlag0, start_I2C;

extern char buf2[100];
extern volatile uint8_t User_key;

//data flash
#define DATA_FLASH_PAGESIZE 512
#define FLASH_SIZE 128*1024
//#define DATA_FLASH_BASE 0x0001F000
#define DATA_FLASH_BASE (FLASH_SIZE-10*3*DATA_FLASH_PAGESIZE-DATA_FLASH_PAGESIZE)  //0x1D600//0x1C200
#define USE_DATA_FLASH 1
#define RTC_Power_ADC 1


#define KEY_ADDR    0x20003FFC  /* The location of signature */
#define SIGNATURE   0x21557899  /* The signature word is used by AP code to check if simple LD is finished */

#define CONFIG0_TEST_CODE   0xFFFFFFFF
//#define CONFIG0_TEST_CODE   0xF8FFFFFE
#define CONFIG1_TEST_CODE   0x0001F000

#define CONFIG_BASE     0x00300000
#define LDROM_BASE      0x00100000
#define PAGE_SIZE       512
#define VersionID       0x1a2b3c4d
extern int tail_sheet, head_sheet, cur_sheet;
extern volatile uint8_t bPrinter_status, bPrinter_error;

#define SYS_DEBUG 1
#define LCD_DEBUG 1
#define FLASH_DEBUG 1
#endif
